

const base_url = main_config.base_url;
$.ajax({
  url: `${base_url}/getAllDataBses`,
  // async: false,
  cache: false,
  success: function (data) {
    console.log(data)
    let div_main = $('.div_database');
    div_main.empty();
    let div_tables = $('.div_tables');
    div_tables.empty();
    let div_detail = $('.div_detail_table');
    div_detail.empty();
    let pre_code = $('.sql');
    pre_code.empty();
    div_main.append(`<h1>数据库列表</h1>`)
    div_main.append("<table class='div_database_tab'>"
      + "<thead>"
      + " <th>编号</th>"
      + " <th>数据库名称</th>"
      + " <th>操作</th>"
      + " </thead>"
      + " <tbody class='div_database_tab_tb'></tbody>"
      + " </table>")
    div_main.appendTo('.div_database');
    let div_database_tab_tb = $('.div_database_tab_tb');
    div_database_tab_tb.empty();
    // div_database_tab_tb.append("<tr><td>1</td><td>2</td><td><button>查看表格</button></td></tr>");
    // div_database_tab_tb.appendTo('.div_database_tab_tb')
    for (let index = 0; index < data.length; index++) {
      const element = data[index];
      div_database_tab_tb.append(`<tr><td>${index + 1}</td><td>${element['Database']}</td><td><button onclick="getTablesByDataBase('${element['Database']}')">查看</button></td></tr>`);
      div_database_tab_tb.appendTo('.div_database_tab_tb');
    }


  },
  error: function (err) {
    console.log(err.message);
  }
})

function getTablesByDataBase(database) {
  // console.log(database);
  $.ajax({
    url: `${base_url}/getTablesByDataBase?db_name=${database}`,
    // async: false,
    cache: false,
    success: function (data) {
      // console.log(data)
      let div_main = $('.div_tables');
      div_main.empty();
      let div_detail = $('.div_detail_table');
      div_detail.empty();
      let pre_code = $('.sql');
      pre_code.empty();
      div_main.append(`<h1>数据库(${database})表格列表(共：${data.length}个)</h1>`)
      div_main.append(`<a onclick='exportTablesStructure("${data[0]['table_schema']}")' class='export_button_excel' id='export_button'>导出表格结构文档（excel）</a>`);
      // div_main.append(`<a onclick='exportTablesData("${data[0]['table_schema']}")' class='export_button_data'  id='export_button'>导出数据</a>`);
      div_main.append(`<a onclick='exportTablesSQLNoData("${data[0]['table_schema']}")' class='export_button_sql_nodata'  id='export_button'>导出SQL(不带数据)</a>`);
      // div_main.append(`<a onclick='exportTablesSQLData("${data[0]['table_schema']}")' class='export_button_sql_data'  id='export_button'>导出SQL(带数据)</a>`);

      div_main.appendTo('.div_tables');
      div_main.append("<table class='div_tables_tab'>"
        + "<thead>"
        + " <th>编号</th>"
        + " <th><input type='checkbox' class='all' onclick='changeCheckboxState()'  />全选</th>"
        + " <th>表格名称</th>"
        + " <th>描述</th>"
        + " <th>操作</th>"
        + " </thead>"
        + " <tbody class='div_tables_tab_tb'></tbody>"
        + " </table>")
      div_main.appendTo('.div_tables');
      let div_database_tab_tb = $('.div_tables_tab_tb');
      div_database_tab_tb.empty();
      // div_database_tab_tb.append("<tr><td>1</td><td>2</td><td><button>查看表格</button></td></tr>");
      // div_database_tab_tb.appendTo('.div_database_tab_tb')
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        div_database_tab_tb.append(`<tr><td>${index + 1}</td>
                        <td><input name="table_name" class="table_name" type="checkbox" value="${element['table_name']},${element['table_comment']}" onclick='checkAllTrue()' /></td>
                        <td>${element['table_name']}</td>
                        <td>${element['table_comment']}</td>
                        <td>
                        <button onclick="getDetailByTabName('${element['table_schema']}', '${element['table_name']}', '${element['table_comment']}')">查看表格结构</button>`
          // <button onclick="getOneTableData('${element['table_schema']}', '${element['table_name']}', 1)">查看数据</button>
          + `</td></tr>`);
        div_database_tab_tb.appendTo('.div_tables_tab_tb');
      }

      // div_main.append(`<button onclick='exportTables()' class='export_button' >导出选中表格结构</button>`);

    },
    error: function (err) {
      console.log(err.message)
    }
  })
}


function getDetailByTabName(database, tab_name, table_comment) {
  // hljs.initHighlightingOnLoad();
  console.log(database);
  $.ajax({
    url: `${base_url}/getDetailByTabName?db_name=${database}&tab_name=${tab_name}`,
    async: false,
    cache: false,
    complete: function (data) {
      // const a = hljs.initHighlightingOnLoad('aasdas');
      // console.log(a)
    },
    success: function (data) {

      // console.log(data)
      let ddl = data.ddl;
      // console.log(ddl.match(/`(\S*)`/g))
      const strs = ddl.match(/`(\S*)`/g);
      const strs2 = ddl.match(/'(\S*)'/g);
      for (const it of strs) {
        // console.log(it)
        const reg = new RegExp(it)
        ddl = ddl.replace(reg, '<span style="color:#2196F3;">' + it + '</span>')
      }
      for (const it of strs2) {
        const reg = new RegExp(it)
        ddl = ddl.replace(reg, '<span style="color:#f44336;">' + it + '</span>')
      }
      // while(dd)
      // ddl = ddl.replace(/`(\S*)`/g, '`<span style="color:#2196F3;">'+ddl.match(/`(\S*)`/g)+'</span>`')
      // console.log(ddl)
      data = data.table_columns;

      let div_main = $('.div_detail_table');
      div_main.empty();
      let pre_code = $('.sql');
      pre_code.empty();
      div_main.css("display", '');
      div_main.append(`<h1>数据库(${database})：${table_comment}(${tab_name})表格详情</h1>`)
      div_main.append("<table class='div_detail_tab'>"
        + "<tr  style='font-size:30px;font-weight:bold;text-align:center;'>" + `<td   style='font-weight:bold;font-size:18px;' colspan='5'>${table_comment}(${tab_name})</td>` + "</tr>"
        + "<tr style='text-align:center;'>"
        + " <td  style='font-weight:bold;font-size:16px;'>编号</td>"
        + " <td  style='font-weight:bold;font-size:16px;'>列名</td>"
        + " <td  style='font-weight:bold;font-size:16px;'>数据类型</td>"
        + " <td  style='font-weight:bold;font-size:16px;'>是否可空</td>"
        + " <td  style='font-weight:bold;font-size:16px;'>描述</td>"
        // + " <th>操作</th>"
        + " </tr>"
        // + " <tbody class='div_detail_tab_tb'></tbody>"
        + " </table>")
      div_main.appendTo('.div_detail_table');
      let div_database_tab_tb = $('.div_detail_tab');
      // div_database_tab_tb.empty();
      // div_database_tab_tb.append("<tr><td>1</td><td>2</td><td><button>查看表格</button></td></tr>");
      // div_database_tab_tb.appendTo('.div_database_tab_tb')
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        div_database_tab_tb.append(`<tr><td>${index + 1}</td>
                        <td>${element['column_name']}</td>
                        <td>${element['column_type']}</td>
                        <td>${element['is_nullable']}</td>
                        <td>${element['column_comment']}</td></tr>`);
        div_database_tab_tb.appendTo('.div_detail_tab_tb');
      }
      // div_main.append('<hr>');
      // div_main.append('<pre><code class="sql"></code></pre>')
      div_main.appendTo('.div_detail_table');
      let code = $('.sql');
      code.append(ddl);
      code.appendTo('.sql');
      div_main.appendTo('.div_detail_table')

    },
    error: function (err) {
      console.log(err.message);
    }
  })

}

// function keyUp()
$('#page_num_input').bind('keyup', function (event) {
  console.log(111)
  if (event.keyCode == "13") {
    console.log(222)
    //回车执行查询
    //$('#search_button').click();
    let data = localStorage.getItem('data')
    data = JSON.parse(data);
    getOneTableData(data.database, data.tab_name, $('#page_num_input').val());
  }
});
function getOneTableData(database, tab_name, page_num) {

  $.ajax({
    url: `${base_url}/getOneTableData?db_name=${database}&tab_name=${tab_name}&page_num=${page_num}`,
    async: false,
    cache: false,
    success: function (data) {
      console.log(data)

      let div_main = $('.div_tab_data');
      // div_main.css("display", 'none');
      div_main.empty();
      div_main.append(`<h1>数据库：${database}</h1>`)
      // div_main.append(`<table><tr  style='font-size:30px;font-weight:bold;text-align:center;'><td colspan='5'>${database}数据库表格数据</td></tr></table>`);
      div_main.append(`<table></table>`);
      // div_main.appendTo('.div_detail');
      // for (let y = 0; y < data.length; y++) {
      // const ele = data[y];
      const columns = data.columns;
      const tab_name = data.tab_name;
      const data_ele = data.data;
      localStorage.setItem('data', JSON.stringify({ database, tab_name }));
      div_main.append(`
                <input id='page_num_input' style='width:50px;text-align:right;' type='text' />
                `)

      div_main.append("<table class='div_detail_tab" + 1 + "' style='"
        + "border:1px solid #6fcdfe;border-collapse: collapse;font-size: 20px;'>"
        + "<tr  style='"
        + "background-color: #93DAFF;"
        + "background: -o-linear-gradient(90deg, #93DAFF, #c1e9fe);"
        + "background: -moz-linear-gradient(center top, #93DAFF 5%, #c1e9fe 100%);"
        + "background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #93DAFF), color-stop(1, #c1e9fe));"
        + "filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#93DAFF', endColorstr='#c1e9fe');"
        + "color: #000000;"
        + "font-size:20px;font-weight:bold;text-align:center;"
        // +"font-size:20px;font-weight:bold;text-align:center;background:gray;"
        + "'>" + `<td colspan='${columns.length}'>${data.table_comment}(${data['tab_name']})</td>` + "</tr>"
        + "<tr style='text-align:center;' class='div_detail_tab_th" + 1 + "'>"
        // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>编号</td>"
        // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>列名</td>"
        // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>数据类型</td>"
        // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>是否可空</td>"
        // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>描述</td>"
        // // + " <th>操作</th>"
        + " </tr>"
        // + " <tbody class='div_detail_tab_tb" + y + "'></tbody>"
        + " </table>")
      div_main.append(`<table></table>`);
      div_main.appendTo('.div_tab_data');


      let div_detail_tab_tr_class = ".div_detail_tab_th" + 1
      let div_detail_tab_tr = $(div_detail_tab_tr_class);

      for (let x = 0; x < columns.length; x++) {
        const element = columns[x];
        div_detail_tab_tr.append(`
                        <td style='
                        background-color: #93DAFF;
                        background: -o-linear-gradient(90deg, #93DAFF, #c1e9fe);
                        background: -moz-linear-gradient(center top, #93DAFF 5%, #c1e9fe 100%);
                        background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #93DAFF), color-stop(1, #c1e9fe));
                        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#93DAFF', endColorstr='#c1e9fe');
                        color: #000000;
                        font-size:16px;
                        '>${element.column_name === 'id' ? '编号' : (!element.column_comment ? element.column_name : element.column_comment)}</td>
                        `);
      }
      let div_database_tab_tb_class = '.div_detail_tab' + 1;
      let div_database_tab_tb = $(div_database_tab_tb_class);

      // const page_count = data.page_count === 0 ?  data.page_count
      for (let i = 0; i < data_ele.length; i++) {
        div_database_tab_tb.append(`<tr class='div_detail_tab_${tab_name}_${i}'></tr>`)
        let div_detail_tab_i_class = `.div_detail_tab_${tab_name}_${i}`;
        let div_detail_tab_i = $(div_detail_tab_i_class);
        const element = data_ele[i];
        for (let z = 0; z < columns.length; z++) {
          const element_column = columns[z];
          div_detail_tab_i.append(`
                                <td style='border:1px solid #6fcdfe;border-collapse: collapse;font-size: 14px;'>${element[element_column['column_name']]}</td>
                                `)
        }
      }

      div_main.append(`
                    <a onclick="getOneTableData('${database}', '${tab_name}', 1)" class='export_button_excel' id='export_button'>首页</a>
                    <a onclick="getOneTableData('${database}', '${tab_name}', ${Number(page_num) - 1})" class='export_button_excel' id='export_button'>上一页</a>
                    <input id='page_num_input' style='width:50px;text-align:right;' type='text' value=${page_num} />
                    /
                    ${data.page_count === 0 ? 1 : data.page_count}页
                    <a onclick="getOneTableData('${database}', '${tab_name}', ${Number(page_num) + 1})" class='export_button_excel' id='export_button'>下一页</a>
                    <a onclick="getOneTableData('${database}', '${tab_name}', ${data.page_count})" class='export_button_excel' id='export_button'>尾页</a>
                    &nbsp;共${data.count}条
                `)
      div_main.appendTo('.div_tab_data');
      //绑定回车事件
      $('#page_num_input').bind('keyup', function (event) {
        if (event.keyCode == "13") {
          //回车执行查询
          //$('#search_button').click();
          let data = localStorage.getItem('data')
          data = JSON.parse(data);
          getOneTableData(data.database, data.tab_name, $('#page_num_input').val());
        }
      });
      // }
    },
    error: function (err) {
      console.log(err.message);
    }
  })
}
function exportTablesData(database) {
  let checkboxs = $('.table_name');
  let checkboxTrues = []
  let tablesCN = [];
  for (let i = 0; i < checkboxs.length; i++) {
    const element = checkboxs[i];
    if (element.checked) {
      let values = element.value.split(',');
      tablesCN.push(values[1]);
      checkboxTrues.push(values[0])
    }
  }
  if (checkboxTrues.length == 0) {
    alert('请选择需要导出的表格！');
    return;
  }
  checkboxTrues = JSON.stringify(checkboxTrues);
  $.ajax({
    url: `${base_url}/getTablesData?db_name=${database}&tab_names=${checkboxTrues}`,
    async: false,
    cache: false,
    success: function (data) {
      console.log(data)

      let div_main = $('.div_detail');
      // div_main.css("display", 'none');
      div_main.empty();
      div_main.append(`<table><tr  style='font-size:30px;font-weight:bold;text-align:center;'><td colspan='5'>${database}数据库表格数据</td></tr></table>`);
      div_main.append(`<table></table>`);
      // div_main.appendTo('.div_detail');
      for (let y = 0; y < data.length; y++) {
        const ele = data[y];
        const columns = ele.columns;
        const tab_name = ele.tab_name;
        const data_ele = ele.data;
        div_main.append("<table class='div_detail_tab" + y + "' style='border:1px solid black;border-collapse: collapse;font-size: 20px;'>"
          + "<tr  style='font-size:20px;font-weight:bold;text-align:center;background:gray;'>" + `<td colspan='5'>${y + 1}.${tablesCN[y]}(${ele['tab_name']})</td>` + "</tr>"
          + "<tr style='text-align:center;' class='div_detail_tab_th" + y + "'>"
          // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>编号</td>"
          // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>列名</td>"
          // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>数据类型</td>"
          // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>是否可空</td>"
          // + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>描述</td>"
          // // + " <th>操作</th>"
          + " </tr>"
          // + " <tbody class='div_detail_tab_tb" + y + "'></tbody>"
          + " </table>")
        div_main.append(`<table></table>`);
        div_main.appendTo('.div_detail');


        let div_detail_tab_tr_class = ".div_detail_tab_th" + y
        let div_detail_tab_tr = $(div_detail_tab_tr_class);

        for (let x = 0; x < columns.length; x++) {
          const element = columns[x];
          div_detail_tab_tr.append(`
                        <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>${element.column_name === 'id' ? '编号' : (!element.column_comment ? '' : element.column_comment)}</td>
                        `);
        }
        let div_database_tab_tb_class = '.div_detail_tab' + y;
        let div_database_tab_tb = $(div_database_tab_tb_class);
        const column_length = columns.length;
        for (let i = 0; i < data_ele.length; i++) {
          const element_one = data_ele[i];
          div_database_tab_tb.append(`<tr class='div_detail_tab_${tab_name}_${i}'></tr>`)
          let div_detail_tab_i_class = `.div_detail_tab_${tab_name}_${i}`;
          let div_detail_tab_i = $(div_detail_tab_i_class);
          for (let z = 0; z < columns.length; z++) {
            const element_column = columns[z];
            div_detail_tab_i.append(`
                            <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element_one[element_column['column_name']]}</td>
                            `)
          }
        }

        // div_database_tab_tb.empty();
        // div_database_tab_tb.append("<tr><td>1</td><td>2</td><td><button>查看表格</button></td></tr>");
        // div_database_tab_tb.appendTo('.div_database_tab_tb')
        // const table_data = ele['data'];
        // for (let index = 0; index < table_data.length; index++) {
        //     const element = table_data[index];
        //     div_database_tab_tb.append(`<tr><td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${index + 1}</td>
        //     <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['column_name']}</td>
        //     <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['column_type']}</td>
        //     <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['is_nullable']}</td>
        //     <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['column_comment']}</td></tr>`);
        //     div_database_tab_tb.appendTo(div_database_tab_tb_class);
        // }
      }
      return;
      // const tables = document.getElementsByTagName("table");
      // const tables = document.getElementsByClassName('.div_detail_tab');
      // const tables = $('.div_detail_tab');

      const tables = $('.div_detail');
      let html = "<html><head><meta charset='utf-8' /></head><body>";
      // for (let i = 0; i < tables.length; i++) {
      //     const element = tables[i];
      //     html += element.outerHTML + "<br/>";
      // }
      html += tables[0].outerHTML;
      html += "</body></html>";
      // 使用outerHTML属性获取整个table元素的HTML代码（包括<table>标签），然后包装成一个完整的HTML文档，设置charset为urf-8以防止中文乱码
      // var html = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementsByTagName("table")[0].outerHTML + "<br/>"
      //     + document.getElementsByTagName("table")[1].outerHTML + "</body></html>";
      // 实例化一个Blob对象，其构造函数的第一个参数是包含文件内容的数组，第二个参数是包含文件类型属性的对象
      var blob = new Blob([html], { type: "application/vnd.ms-excel" });
      var a = document.getElementsByTagName("a")[0];
      // let export_button = document.getElementsByClassName('export_button');
      // 利用URL.createObjectURL()方法为a元素生成blob URL
      a.href = URL.createObjectURL(blob);
      // 设置文件名
      a.download = "表格结构.xlsx";

    },
    error: function (err) {
      console.log(err.message);
    }
  })
}
function exportTablesSQLNoData(database) {
  let checkboxs = $('.table_name');
  let checkboxTrues = []
  let tablesCN = [];
  for (let i = 0; i < checkboxs.length; i++) {
    const element = checkboxs[i];
    if (element.checked) {
      let values = element.value.split(',');
      tablesCN.push(values[1]);
      checkboxTrues.push(values[0])
    }
  }
  if (checkboxTrues.length == 0) {
    alert('请选择需要导出的表格！');
    return;
  }
  console.log(checkboxTrues);
  checkboxTrues = JSON.stringify(checkboxTrues);
  $.ajax({
    url: `${base_url}/getSqlByTabNames?db_name=${database}&tab_names=${checkboxTrues}`,
    async: false,
    cache: false,
    success: function (data) {
      console.log(data)
      let sql_text = '';
      for (let i = 0; i < data.length; i++) {
        sql_text += `
${data[i].ddl}

                `;
      }
      console.log(sql_text)

      exportRaw(new Date().getTime() + '.sql', sql_text);

    },
    error: function (err) {
      console.log(err.message);
    }
  })
}

function fakeClick(obj) {
  var ev = document.createEvent("MouseEvents");
  ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  obj.dispatchEvent(ev);
}

function exportRaw(name, data) {
  var urlObject = window.URL || window.webkitURL || window;
  var export_blob = new Blob([data]);
  var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
  save_link.href = urlObject.createObjectURL(export_blob);
  save_link.download = name;
  fakeClick(save_link);
}

/**
 * 导出选中表格结构
 * 
 */
function exportTablesStructure(database) {
  let checkboxs = $('.table_name');
  let checkboxTrues = []
  let tablesCN = [];
  for (let i = 0; i < checkboxs.length; i++) {
    const element = checkboxs[i];
    if (element.checked) {
      let values = element.value.split(',');
      tablesCN.push(values[1]);
      checkboxTrues.push(values[0])
    }
  }
  if (checkboxTrues.length == 0) {
    alert('请选择需要导出的表格！');
    return;
  }
  console.log(checkboxTrues);
  checkboxTrues = JSON.stringify(checkboxTrues);
  $.ajax({
    url: `${base_url}/getDetailByTabNames?db_name=${database}&tab_names=${checkboxTrues}`,
    async: false,
    cache: false,
    success: function (data) {
      console.log(data)
      let div_main = $('.div_detail');
      div_main.css("display", 'none');
      div_main.empty();
      div_main.append(`<table><tr  style='font-size:30px;font-weight:bold;text-align:center;'><td colspan='5'>${database}数据库表格结构</td></tr></table>`);
      div_main.append(`<table></table>`);
      // div_main.appendTo('.div_detail');
      for (let y = 0; y < data.length; y++) {
        const ele = data[y];
        div_main.append("<table class='div_detail_tab" + y + "' style='border:1px solid black;border-collapse: collapse;font-size: 20px;'>"
          + "<tr  style='font-size:20px;font-weight:bold;text-align:center;background:gray;'>" + `<td colspan='5'>${y + 1}.${tablesCN[y]}(${ele['tab_name']})</td>` + "</tr>"
          + "<tr style='text-align:center;'>"
          + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>编号</td>"
          + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>列名</td>"
          + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>数据类型</td>"
          + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>是否可空</td>"
          + "  <td style='border:1px solid black;border-collapse: collapse;font-size: 18px;font-weight:bold;'>描述</td>"
          // + " <th>操作</th>"
          + " </tr>"
          // + " <tbody class='div_detail_tab_tb" + y + "'></tbody>"
          + " </table>")
        div_main.append(`<table></table>`);
        div_main.appendTo('.div_detail');

        let div_database_tab_tb_class = '.div_detail_tab' + y;
        let div_database_tab_tb = $(div_database_tab_tb_class);
        // div_database_tab_tb.empty();
        // div_database_tab_tb.append("<tr><td>1</td><td>2</td><td><button>查看表格</button></td></tr>");
        // div_database_tab_tb.appendTo('.div_database_tab_tb')
        const table_data = ele['data'];
        for (let index = 0; index < table_data.length; index++) {
          const element = table_data[index];
          div_database_tab_tb.append(`<tr><td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${index + 1}</td>
                        <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['column_name']}</td>
                        <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['column_type']}</td>
                        <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['is_nullable']}</td>
                        <td style='border:1px solid black;border-collapse: collapse;font-size: 16px;'>${element['column_comment']}</td></tr>`);
          div_database_tab_tb.appendTo(div_database_tab_tb_class);
        }
      }


      // const tables = document.getElementsByTagName("table");
      // const tables = document.getElementsByClassName('.div_detail_tab');
      // const tables = $('.div_detail_tab');

      const tables = $('.div_detail');
      let html = "<html><head><meta charset='utf-8' /></head><body>";
      // for (let i = 0; i < tables.length; i++) {
      //     const element = tables[i];
      //     html += element.outerHTML + "<br/>";
      // }
      html += tables[0].outerHTML;
      html += "</body></html>";
      // 使用outerHTML属性获取整个table元素的HTML代码（包括<table>标签），然后包装成一个完整的HTML文档，设置charset为urf-8以防止中文乱码
      // var html = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementsByTagName("table")[0].outerHTML + "<br/>"
      //     + document.getElementsByTagName("table")[1].outerHTML + "</body></html>";
      // 实例化一个Blob对象，其构造函数的第一个参数是包含文件内容的数组，第二个参数是包含文件类型属性的对象
      var blob = new Blob([html], { type: "application/vnd.ms-excel" });
      var a = document.getElementsByTagName("a")[0];
      // let export_button = document.getElementsByClassName('export_button');
      // 利用URL.createObjectURL()方法为a元素生成blob URL
      a.href = URL.createObjectURL(blob);
      // 设置文件名
      const time = new Date().format("yyyyMMddhhmmss");
      a.download = `${time}.xls`;

    },
    error: function (err) {
      console.log(err.message);
    }
  })
}
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                 //月份 
    "d+": this.getDate(),                    //日 
    "h+": this.getHours(),                   //小时 
    "m+": this.getMinutes(),                 //分 
    "s+": this.getSeconds(),                 //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds()             //毫秒 
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}

/**
 * 全选/反选
 * 
 */
function changeCheckboxState() {
  let all = $(".all");
  if (all[0].checked) {
    $(".table_name").prop("checked", true);
  } else {
    $(".table_name").prop("checked", false);
  }
}
/**
 * 当全选状态时，取消某一个checkbox，解除全选状态
 * 
 */
function checkAllTrue() {
  let all = $(".all");
  if (all[0].checked) {
    $(".all").prop("checked", false);
  }
}


